package com.yingxin.yxerp.module.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.enums.task.DynamicTaskLogCategoryEnum;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yxerp.module.report.entity.DynamicTaskLog;
import com.yingxin.yxerp.module.report.mapper.DynamicTaskLogMapper;
import com.yingxin.yxerp.module.report.service.IDynamicTaskLogService;
import com.yingxin.yxerp.module.report.vo.resp.DynamicTaskLogRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 定时任务 执行记录 服务类 实现类
 * @author yuebao
 * @date 2023-11-25
 */

@Service
public class DynamicTaskLogServiceImpl
        extends ServiceImpl<DynamicTaskLogMapper, DynamicTaskLog>
        implements IDynamicTaskLogService {

    /**
     * 获取 mapper
     */
    @Override
    public DynamicTaskLogMapper mapper(){
        return baseMapper;
    }

    /**
     * 保存
     */
    @Override
    public void saveLog( DynamicTaskLog log ){
        if( StringUtils.isEmpty( log.getTaskId() ) ){
            log.setId( IdHelper.snowflakeId() );
        }
        if( StringUtils.isEmpty( log.getCategory() ) ){
            log.setCategory( DynamicTaskLogCategoryEnum.AUTO.getType() );
        }
        if( StringUtils.isEmpty( log.getTaskId() ) ||
                StringUtils.isEmpty( log.getTaskType() ) ||
                StringUtils.isEmpty( log.getTaskName() ) ){
            throw new BizException("任务不能为空！");
        }

        save( log );
    }

    /**
     * 根据 id 查询
     */
    @Override
    public DynamicTaskLogRespVo getVO( String id ){
        return mapper().getVo( id );
    }

}
